package com.example.farmer.Fragment.Message;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Entity.ChatMessage;
import com.example.farmer.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class InboxMessageAdapter extends RecyclerView.Adapter<InboxMessageAdapter.ViewHolder> {

    private List<ChatMessage> messages;
    private Context context;
    private Set<String> displayedSenders = new HashSet<>();

    public InboxMessageAdapter(List<ChatMessage> messages, Context context) {
        this.messages = messages;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.received_message_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ChatMessage message = messages.get(position);
        String sender = message.getSender();

        if (!displayedSenders.contains(sender)) {
            holder.timestamp.setText(formatTimestamp(message.getTimestamp())); // 显示时间
        holder.senderText.setText(hideMiddleFourDigits(message.getSender())); // 绑定发送者信息
            holder.itemView.setOnClickListener(v -> {
                // 传递发送者号码到详细消息Activity
                Context context = v.getContext();
                Intent intent = new Intent(context, MessageDetailActivity.class);
                intent.putExtra("sender", sender);
                context.startActivity(intent);
            });
            displayedSenders.add(sender); // 标记为已显示
        } else {
            // 如果消息已显示过，不绑定任何数据，避免重复显示
            holder.timestamp.setVisibility(View.GONE);
            holder.senderText.setVisibility(View.GONE);
            holder.userimage.setVisibility(View.GONE); // 或者设置为null图片
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    private String formatTimestamp(long timestamp) {
        return new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView userimage;
        TextView senderText;
        TextView timestamp;

        public ViewHolder(View itemView) {
            super(itemView);
            userimage = itemView.findViewById(R.id.user_image);
            timestamp = itemView.findViewById(R.id.timestamp);
            senderText = itemView.findViewById(R.id.user_number); // 假设布局中定义了senderText
        }
    }

    private String hideMiddleFourDigits(String number) {
        if (number == null || number.length() < 8) {
            // 如果手机号长度不足8位，直接返回原手机号或提示信息
            return "Invalid number";
        }
        // 取手机号的前三位和后四位，中间用星号替换
        return number.substring(0, 3) + "****" + number.substring(number.length() - 4);
    }
}